package com.sztx.worker_monitor.common;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * <p>Operations on {@link java.lang.String} that are
 * {@code null} safe.</p>
 */
public class StringUtils {

    /** The empty String {@code ""}. */
    public static final String EMPTY = "";

    /** Represents a failed index search.  */
    public static final int INDEX_NOT_FOUND = -1;
    
    private static String CAMEL_REGEX = "(?<!^)(?=[A-Z])";

    /**
     * Split camel string to array. 
     */
	public static String[] splitCamel(String camel) {
		return camel.split(CAMEL_REGEX);
	}
    
    /**
     * Constructs a new String by decoding the specified array of bytes using the specified charset. 
     * The length of the new String is a function of the charset, 
     * and hence may not be equal to the length of the byte array. 
     * @param bts
     * @param charset
     * @return
     * @since 1.0
     */
    public static String toString(byte[] bts, String charset) {
    	if (bts == null) {
            return null;
        }
    	return new String(bts, CharsetUtils.toCharset(charset));
    }
    
    /**
     * Calls {@link String#getBytes(Charset)}
     * @param string The string to encode (if null, return null).
     * @param charset The {@link Charset} to encode the {@code String}
     * @return the encoded bytes
     * @since 1.0
     */
    public static byte[] getBytes(String string, String charset) {
    	if (string == null) {
            return null;
        }
        return string.getBytes(CharsetUtils.toCharset(charset));
    }
    
    /**
     * Calls {@link String#getBytes(Charset)}
     *
     * @param string
     *            The string to encode (if null, return null).
     * @param charset
     *            The {@link Charset} to encode the {@code String}
     * @return the encoded bytes
     */
    public static byte[] getBytes(String string, Charset charset) {
        if (string == null) {
            return null;
        }
        return string.getBytes(charset);
    }

    /**
     * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is null, empty or whitespace
     * @since 1.0
     */
    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is
     *  not empty and not null and not whitespace
     * @since 1.0
     */
    public static boolean isNotBlank(CharSequence cs) {
        return !StringUtils.isBlank(cs);
    }
    
  //金钱格式转化
  	public static String moneyChange(String string)
  	{
  		if("".equals(string))
  		{
  			return "0.00";
  		}
  		
  		BigDecimal digit = new BigDecimal(string).divide(new BigDecimal("100"));
  		DecimalFormat df = new DecimalFormat("###,##0.00");
  		return df.format(digit);
  	}
  	
  	public static Integer safeInteger(String str){
  	 	return isBlank(str) ? null : Integer.valueOf(str);
  	}
  	/** 
     * 手机号验证 
     *  
     * @param  str 
     * @return 验证通过返回true 
     */  
    public static boolean isMobile(String str) {   
        Pattern p = null;  
        Matcher m = null;  
        boolean b = false;   
        p = Pattern.compile("^[1][0-9]{10}$"); // 验证手机号  
        m = p.matcher(str);  
        b = m.matches();   
        return b;  
    }  
    /**
     * 创建短信验证码
     * @param len
     * @return
     */
    public static String creatmobverify(int len){
    	String verify = "";
    	for (int i = 0; i < len; i++) {
    		int max=9;
            int min=0;
            Random random = new Random();
            int s = random.nextInt(max)%(max-min+1) + min;
            verify += s;
		}
    	return verify;
    }
    static int result = 0;
    public static String getNewPackageId() {
        synchronized (lock1) {
          if (result == 100000)
            result = 0;
          else {
            result += 1;
          }
          return sdf.format(new Date()) + getRandom(String.valueOf(result), 5);
        }
   }
    
    private static String getRandom(String str, int needLen) {
        int len = str.trim().length();
        for (int i = len; i < needLen; i++) {
          str = "0" + str;
        }
        return str;
      }
	/**
	 * 将字符串以特定符号为分割符()
	 * 
	 * @param str
	 * @return 
	 */
	public static List<String> splitString(String str, String regex) {
		if (str == null || str.length() == 0) {
			return null;
		}

		String[] strarray=str.split(regex); 
		List<String> list = Arrays.asList(strarray);
		return list;
	}
	

	
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
   private static Object lock1 = new Object();
}